﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FantastPlatform.Framework;
using FantastPlatform.Community.IService;
using FantastPlatform.Framework.Core;
using FantastPlatform.Community.IMode;
using FantastPlatform.Community.Model;
using FantastPlatform.Framework.Model;

namespace FantastPlatform.Community.UI.Service
{
    public partial class NoticeService : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoginEmployee = Session["User"] as FantastPlatform.Framework.Model.Employee;
            INoticeService noticeService = ServiceFactory.CreateService<INoticeService>();

            ConditionGroup group = new ConditionGroup();
            int totalCount = 0;
            switch (Action)
            {
                case "Add":
                    string data = Data;
                    INotice notice = (INotice)JsonToEntity<FantastPlatform.Community.Model.Notice>(RepairString(data));                   
                  
                    if (notice.Id == 0)
                    { 
                        notice.Enable = true;
                        notice.CreatePolice = new Employee { Id = LoginEmployee.Id };
                        notice.CreateDate = DateTime.Now;
                        noticeService.Create(notice);
                        ReturnSuccessMessage("保存通知成功");
                    }
                    else
                    {
                        INotice OldNotice = noticeService.Get(notice.Id);
                        OldNotice.Name = notice.Name;
                        OldNotice.Content = notice.Content;
                        noticeService.Update(OldNotice);
                        ReturnSuccessMessage("更新通知成功");
                    }
                    break;
                case "Search":
                    Condition enableCondition = new Condition() { Type = typeof(bool).FullName, Field = "Enable", Value1 = true };
                    group.Conditions.Add(enableCondition);
                    IList<Sort> sorts = new List<Sort>();
                    sorts.Add(new Sort() { Field = "CreateDate", Order = Orders.Asc });
                    IList<INotice> noticeList = noticeService.Search(group, sorts, PageSize, SelfPage, out totalCount);
                    var obj = from n in noticeList
                              select new
                              {
                                  Id = n.Id,
                                  Name = n.Name,
                                  Content = n.Content,
                                  PoliceName = n.CreatePolice.Name,
                                  CreateDate = n.CreateDate.ToString(),
                                  Unit = (from p in n.CreatePolice.EmployeePosts
                                          where p.Enable == true
                                          select p.Post.Organization.Name).FirstOrDefault(),
                                  PoliceCode = n.CreatePolice.Code
                              };
                    ReturnList(totalCount, obj.ToList());
                    break;
                case "SearchByCondition":
                    string Searchjson = RepairString(Data);
                    notice = JsonToEntity<FantastPlatform.Community.Model.Notice>(Searchjson);
                    enableCondition = new Condition() { Type = typeof(bool).FullName, Field = "Enable", Value1 = true };
                    Condition condition1 = new Condition() { Type = typeof(string).FullName, Field = "Name", Value1 = '%' + notice.Name + '%', Comparison = Comparisons.Like, Relation = Relations.And };
                    group.Conditions.Add(enableCondition);
                    group.Conditions.Add(condition1);
                    sorts = new List<Sort>();
                    sorts.Add(new Sort() { Field = "CreateDate", Order = Orders.Asc });
                    noticeList = noticeService.Search(group, sorts, PageSize, SelfPage, out totalCount);
                     obj = from n in noticeList
                          select new
                          {
                              Id = n.Id,
                              Name = n.Name,
                              Content = n.Content,
                              PoliceName = n.CreatePolice.Name,
                              CreateDate = n.CreateDate.ToString(),
                              Unit = (from p in n.CreatePolice.EmployeePosts
                                          where p.Enable ==true
                                          select p.Post.Organization.Name).FirstOrDefault(),                             
                              PoliceCode = n.CreatePolice.Code
                          };
                    var objList = obj.ToList();
                    ReturnList(objList.Count, objList);
                    break;
                case "Get":
                    notice = noticeService.Get(Id);
                    object objNotice = (new
                                     {
                                         CreatePoliceManNumber = notice.CreatePolice.Code,
                                         Name = notice.Name,
                                         Content = notice.Content
                                     });
                    ReturnObj(objNotice);
                    break;
                case "Delete":
                    if (string.IsNullOrEmpty(Data))
                    {
                        throw new Exception("传入的通知ID为空");
                    }
                    List<int> idList = new List<int>();
                    string[] idStrList = Data.Split(',');
                    foreach (string id in idStrList)
                    {
                        idList.Add(int.Parse(id));
                    }
                    noticeService.Delete(idList);
                    ReturnSuccessMessage("删除通知成功");
                    break;
            }
        }
    }
}